package helper;

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import model.board.*;

/** Membaca file XML dan memasukkannya ke lokasi pariwisata
 */
public class XMLReader_1
{
    //---------------------------ATTRIBUTE--------------------------------------
    public static String filename;
    public static NodeList ListLP;
    public static LokasiPariwisata [] ArrLP;
    public static int head = 0;

    //--------------------------CONSTRUCTOR-------------------------------------
    @SuppressWarnings("static-access")
    public XMLReader_1 ()
    {
        
    }

    //-----------------------------METHOD---------------------------------------
    public int jum_LP()
    {
        return ListLP.getLength();
    }

    @SuppressWarnings("static-access")
    public static boolean parseToArrLP (String XMLfilename, int jum_wanted_LP)
    {
        ArrLP = new LokasiPariwisata [19];
            String nama = "";
            String kompleks = "";
            String list_rekreasi = "";
            int harga_rekreasi = 0;
            int increase_rekreasi  = 0;
            String list_makanan = "";
            int harga_makanan  = 0;
            int increase_makanan = 0;
            int harga_beli_tanah = 0;
            int harga_beli_villa = 0;
            int harga_beli_hotel = 0;
            int harga_sewa_tanah = 0;
            int harga_sewa_villa = 0;
            int harga_sewa_hotel = 0;
        boolean retval = false;
        try
        {
            File XMLfile = new File (XMLfilename);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(XMLfile);
            doc.getDocumentElement().normalize();
            ListLP = doc.getElementsByTagName("lokasi");
            
            for (int s = 0; s < ListLP.getLength(); s++)
            {
                Node fstNode = ListLP.item(s);
                if (fstNode.getNodeType() == Node.ELEMENT_NODE)
                {
                    Element fstElmnt = (Element) fstNode;
                    //nama
                    NodeList namaElmntLst = fstElmnt.getElementsByTagName("nama");
                    Element namaElmnt = (Element) namaElmntLst.item(0);
                    nama = new String (((Node)namaElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("Nama \t: "+nama);

                    //kompleks
                    NodeList kompleksElmntLst = fstElmnt.getElementsByTagName("kompleks");
                    Element kompleksElmnt = (Element) kompleksElmntLst.item(0);
                    kompleks = new String (((Node)kompleksElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("Kompleks \t: "+kompleks);

                    //list_rekreasi
                    NodeList list_rekreasiElmntLst = fstElmnt.getElementsByTagName("list_rekreasi");
                    Element list_rekreasiElmnt = (Element) list_rekreasiElmntLst.item(0);
                    list_rekreasi = new String (((Node)list_rekreasiElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("list_rekreasi \t: "+list_rekreasi);

                    //harga_rekreasi
                    NodeList harga_rekreasiElmntLst = fstElmnt.getElementsByTagName("harga_rekreasi");
                    Element harga_rekreasiElmnt = (Element) harga_rekreasiElmntLst.item(0);
                    harga_rekreasi = new Integer (((Node)harga_rekreasiElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("harga_rekreasi \t: "+harga_rekreasi);

                    //increase_rekreasi
                    NodeList increase_rekreasiElmntLst = fstElmnt.getElementsByTagName("increase_rekreasi");
                    Element increase_rekreasiElmnt = (Element) increase_rekreasiElmntLst.item(0);
                    increase_rekreasi = new Integer (((Node)increase_rekreasiElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("increase_rekreasi \t: "+increase_rekreasi);

                    //list_makanan
                    NodeList list_makananElmntLst = fstElmnt.getElementsByTagName("list_makanan");
                    Element list_makananElmnt = (Element) list_makananElmntLst.item(0);
                    list_makanan = new String (((Node)list_makananElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("list_makanan \t: "+list_makanan);

                    //harga_makanan
                    NodeList harga_makananElmntLst = fstElmnt.getElementsByTagName("harga_makanan");
                    Element harga_makananElmnt = (Element) harga_makananElmntLst.item(0);
                    harga_makanan = new Integer (((Node)harga_makananElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("harga_makanan \t: "+harga_makanan);

                    //increase_makanan
                    NodeList increase_makananElmntLst = fstElmnt.getElementsByTagName("increase_makanan");
                    Element increase_makananElmnt = (Element) increase_makananElmntLst.item(0);
                    increase_makanan = new Integer (((Node)increase_makananElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("increase_makanan \t: "+increase_makanan);


                    //harga_beli_tanah
                    NodeList harga_beli_tanahElmntLst = fstElmnt.getElementsByTagName("harga_beli_tanah");
                    Element harga_beli_tanahElmnt = (Element) harga_beli_tanahElmntLst.item(0);
                    harga_beli_tanah = new Integer (((Node)harga_beli_tanahElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("Harga Beli Tanah \t: "+harga_beli_tanah);

                    //harga_beli_villa
                    NodeList harga_beli_villaElmntLst = fstElmnt.getElementsByTagName("harga_beli_villa");
                    Element harga_beli_villaElmnt = (Element) harga_beli_villaElmntLst.item(0);
                    harga_beli_villa = new Integer (((Node)harga_beli_villaElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("Harga Beli villa \t: "+harga_beli_villa);

                    //harga_beli_hotel
                    NodeList harga_beli_hotelElmntLst = fstElmnt.getElementsByTagName("harga_beli_hotel");
                    Element harga_beli_hotelElmnt = (Element) harga_beli_hotelElmntLst.item(0);
                    harga_beli_hotel = new Integer (((Node)harga_beli_hotelElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("Harga Beli hotel \t: "+harga_beli_hotel);

                    //harga_sewa_tanah
                    NodeList harga_sewa_tanahElmntLst = fstElmnt.getElementsByTagName("harga_sewa_tanah");
                    Element harga_sewa_tanahElmnt = (Element) harga_sewa_tanahElmntLst.item(0);
                    harga_sewa_tanah = new Integer (((Node)harga_sewa_tanahElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("Harga sewa Tanah \t: "+harga_sewa_tanah);

                    //harga_sewa_villa
                    NodeList harga_sewa_villaElmntLst = fstElmnt.getElementsByTagName("harga_sewa_villa");
                    Element harga_sewa_villaElmnt = (Element) harga_sewa_villaElmntLst.item(0);
                    harga_sewa_villa = new Integer (((Node)harga_sewa_villaElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("Harga sewa villa \t: "+harga_sewa_villa);

                    //harga_sewa_hotel
                    NodeList harga_sewa_hotelElmntLst = fstElmnt.getElementsByTagName("harga_sewa_hotel");
                    Element harga_sewa_hotelElmnt = (Element) harga_sewa_hotelElmntLst.item(0);
                    harga_sewa_hotel = new Integer (((Node)harga_sewa_hotelElmnt.getChildNodes().item(0)).getNodeValue());
                    //System.out.println ("Harga sewa hotel \t: "+harga_sewa_hotel);
                }
                XMLReader.ArrLP[s] = new LokasiPariwisata(nama,kompleks,list_rekreasi,harga_rekreasi,increase_rekreasi,list_makanan,harga_makanan,increase_makanan,harga_beli_tanah,harga_beli_villa,harga_beli_hotel,harga_sewa_tanah,harga_sewa_villa,harga_sewa_hotel);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        if (ListLP.getLength() == jum_wanted_LP)
        {
            head = 0;
            retval =  true;
        }
        else
            System.out.println ("Jumlah Lokasi Pariwisata harus sama dengan "+jum_wanted_LP+"! Perbaiki file!");
        return retval;
    }

    public static void NextHead ()
    {
        if (head == (ListLP.getLength()-1))
            head = 0;
        else
            head +=1;
    }

    //MAIN
    public static void main (String[] args)
    {
        if (XMLReader.parseToArrLP("lokasi_pariwisata.xml", 18))
        {
            System.out.println("Jumlah Lokasi Pariwisata : "+XMLReader.ListLP.getLength());
            for (int i=0; i < 4; i++)
                ArrLP[i].PrintAttribute();
        }
        else
            System.out.println ("Pembacaan file gagal!");
    }
}
